package com.coollang.smartbra.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.SearchActivity;
import com.coollang.smartbra.utils.UIUtils;
import com.inuker.bluetooth.library.search.SearchResult;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 扫描设备
 */
public class DeviceListAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<SearchResult> mDeviceList;
    Map<String, Integer> devicemap = new LinkedHashMap<>();

    private boolean mIsShowNoDeviceUI = false;

    public void setShowNoDeviceUI(boolean showNoDeviceUI) {
        mIsShowNoDeviceUI = showNoDeviceUI;
        notifyDataSetChanged();
    }

    private static final int VIEW_ITEM = 0;
    private static final int VIEW_FOOT = 1;

    public DeviceListAdapter(Context context) {
        mContext = context;
        mDeviceList = new ArrayList<>();
    }

    @Override
    public int getItemCount() {
        return mDeviceList.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView;
        RecyclerView.ViewHolder holder = null;

        if (viewType == VIEW_ITEM) {
            //加载布局文件
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_blelist, parent, false);
            holder = new ItemViewHolder(itemView);
        } else if (viewType == VIEW_FOOT) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.recycle_footview, parent, false);
            holder = new FootViewHolder(itemView);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        int viewType = getItemViewType(position);
        if (viewType == VIEW_ITEM) {
            ((ItemViewHolder) holder).setData(position);
        } else if (viewType == VIEW_FOOT) {
            // 尾巴
            if (mIsShowNoDeviceUI) {
                ((FootViewHolder) holder).mLinearLayout.setVisibility(View.VISIBLE);
            } else {
                ((FootViewHolder) holder).mLinearLayout.setVisibility(View.INVISIBLE);
            }
        }
    }

    //根据不同的数据返回不同的viewType
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return VIEW_FOOT;
        }
        return VIEW_ITEM;
    }

    public void addDevice(SearchResult result) {
        if (!mDeviceList.contains(result)) {
            mDeviceList.add(result);
            devicemap.put(result.getAddress(), result.rssi);
            // Collections.sort(mDeviceList, order);
            // notifyDataSetChanged();
            notifyItemChanged(0, result);
        }
    }

    /**
     * 搜索玩了
     */
    public void scanStop() {
        if (mDeviceList != null && mDeviceList.size() > 0) {
            Collections.sort(mDeviceList, order);
            notifyDataSetChanged();
        }
    }

    // 清空集合
    public void cleanData() {
        mDeviceList.clear();
        notifyDataSetChanged();
    }

    private String mCheckMac;

    // 已绑定的设备MAC 地址
    public void checkMac(String deviceMac) {
        mCheckMac = deviceMac;
        notifyDataSetChanged();
    }

    public SearchResult getPositionData(int position) {
        return mDeviceList.get(position);
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_blelist_postion)
        TextView mBlePosition;
        @BindView(R.id.tv_blename)
        TextView mBleName;
        @BindView(R.id.tv_bind)
        TextView mTvBind;
        @BindView(R.id.iv_unbind)
        ImageView mIvBind;
        @BindView(R.id.device_line)
        View device_line;

        private SearchResult mSearchResult;

        public ItemViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
            AutoUtils.autoSize(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((SearchActivity) mContext).connectDevice(mSearchResult);
                }
            });
        }

        public void setData(int position) {
            mBlePosition.setText(UIUtils.getString(R.string.device) + (position + 1));

            SearchResult searchDevice = mDeviceList.get(position);
            mSearchResult = searchDevice;

            mBleName.setText(searchDevice.getName());

            String address = searchDevice.getAddress().replace(":", "");
            if (!TextUtils.isEmpty(mCheckMac) && address.equals(mCheckMac)) {
                mTvBind.setVisibility(View.VISIBLE);
                mIvBind.setVisibility(View.GONE);
            } else {
                mTvBind.setVisibility(View.GONE);
                mIvBind.setVisibility(View.VISIBLE);
            }

            if (position == mDeviceList.size() - 1) {
                device_line.setVisibility(View.GONE);
            } else {
                device_line.setVisibility(View.VISIBLE);
            }
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.no_device_ui)
        public LinearLayout mLinearLayout;

        public FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            AutoUtils.autoSize(itemView);
        }
    }

    /**
     * 信号排序
     */
    Comparator order = new Comparator<SearchResult>() {
        @Override
        public int compare(SearchResult lhs, SearchResult rhs) {
            return devicemap.get(rhs.getAddress()) - devicemap.get(lhs.getAddress());
        }
    };
}
